package com.zhong.blog.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//public class LoginFilter extends UsernamePasswordAuthenticationFilter {
//    @Override
//    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
//        // 不是post请求抛出异常
//        if (!request.getMethod().equals("POST")) {
//            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
//        }
//        // 判断请求是否是json格式，如果不是直接调用父类
//        if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) {
//            // 把request的json数据转换为Map
//            Map<String, String> loginData = new HashMap<>();
//            try {
//                loginData = new ObjectMapper().readValue(request.getInputStream(), Map.class);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            // 调用父类的getParameter() 方法获取key值
//            String username = loginData.get(this.getUsernameParameter());
//            String password = loginData.get(this.getPasswordParameter());
//            if (username == null) {
//                username = "";
//            }
//            if (password == null) {
//                password = "";
//            }
//            username = username.trim();
//            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
//            this.setDetails(request, authRequest);
//            return this.getAuthenticationManager().authenticate(authRequest);
//        } else {
//            return super.attemptAuthentication(request, response);
//        }
//    }
//}
